<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let input = `6,6
         F,M,F,F,F,F
         F,F,M,F,F,F
         F,F,F,F,F,F
         F,F,F,F,M,F
         F,F,F,F,F,M
         F,F,F,F,F,F`
        let spl = input.split('\n')
        let [x, n] = spl[0].split(',').map(v => parseInt(v.trim()))
        let str_arr = []
        for (let i = 1; i < x + 1; i++) {
            str_arr.push(spl[i].split(',').map(v => v.trim()))
        }
        let max = Math.max(x, n)
        let res = 0
        let tag = false
        for (let i = 0; i < max; i++) {
            let xie_count1 = 0
            let xie_count2 = 0
            let fxie_count1 = 0
            let fxie_count2 = 0
            let h_count = 0
            let s_count = 0

            for (let j = 0; j < max; j++) {
                // 斜着遍历
                if (i + j < x && j < n) {
                    if (str_arr[i + j][j] === 'M') {//下三角
                        xie_count1++
                        if (xie_count1 >= res) {
                            res = xie_count1
                        }
                    } else {
                        xie_count1 = 0
                    }
                }
                if (i + j < n && j < x) {
                    if (str_arr[j][i + j] === 'M') {//上三角
                        xie_count2++
                        if (xie_count2 >= res) {
                            res = xie_count2
                        }
                    } else {
                        xie_count2 = 0
                    }
                }

                // 反斜遍历
                if (x - 1 - i - j >= 0 && j < n && x - 1 - i - j < x) {//上三角                 
                    if (str_arr[x - 1 - i - j][j] === 'M') {
                        fxie_count1++
                        if (fxie_count1 >= res) {
                            res = fxie_count1
                        }
                    } else {
                        fxie_count1 = 0
                    }
                }
                if (x - 1 - j >= 0 && x - 1 - j < x && i + j < n) {//下三角    
                    if (str_arr[x - 1 - j][i + j] === 'M') {
                        fxie_count2++
                        if (fxie_count2 >= res) {
                            res = fxie_count2
                        }
                    } else {
                        fxie_count2 = 0
                    }
                }

                // 横向遍历
                if (i < x && j < n) {
                    if (str_arr[i][j] === 'M') {
                        h_count++
                        if (h_count > res) {
                            res = h_count
                        }
                    } else {
                        h_count = 0
                    }
                }

                // 竖向遍历
                if (i < n && j < x) {
                    if (str_arr[j][i] === 'M') {
                        s_count++
                        if (s_count > res) {
                            res = s_count
                        }
                    } else {
                        s_count = 0
                    }
                }
            }
            if (xie_count1 === max || xie_count2 === max || h_count === max || s_count === max) {
                res = max
                tag = true
            }

            if (tag) {
                break
            }
        }



        console.log(res);





    </script>

</body>

</html>